package com.central.common.exception;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.central.common.utils.MessageUtils;
import lombok.Getter;

/**
 * 自定义业务异常
 */
@Getter
public class BusinessException extends RuntimeException {
	private static final long serialVersionUID = 1L;

	private int code;
	private String msg;

	public BusinessException(BizExceptionEnum bizExceptionEnum) {
		super(bizExceptionEnum.getMessage());
		this.code = bizExceptionEnum.getCode();
		String message = MessageUtils.getMessage(code);
		if (StringUtils.isBlank(message)) {
			message = bizExceptionEnum.getMessage();
		}
		this.msg = message;
	}

	public BusinessException(int code) {
		this.code = code;
		this.msg = MessageUtils.getMessage(code);
	}

	public BusinessException(int code, String... params) {
		this.code = code;
		this.msg = MessageUtils.getMessage(code, params);
	}

	public BusinessException(int code, Throwable e) {
		super(e);
		this.code = code;
		this.msg = MessageUtils.getMessage(code);
	}

	public BusinessException(int code, Throwable e, String... params) {
		super(e);
		this.code = code;
		this.msg = MessageUtils.getMessage(code, params);
	}

	public BusinessException(String msg) {
		super(msg);
		this.code = BizExceptionEnum.SERVER_ERROR.getCode();
		this.msg = msg;
	}

	public BusinessException(String msg, Throwable e) {
		super(msg, e);
		this.code = BizExceptionEnum.SERVER_ERROR.getCode();
		this.msg = msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setCode(int code) {
		this.code = code;
	}
}